package com.jservice.model.code;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.generator.ModelGenerator;
import com.jfinal.plugin.activerecord.generator.TableMeta;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * Created by cwledit on 17/5/25.
 */
public class JQueryGenerator extends ModelGenerator {



    public JQueryGenerator(String modelPackageName, String baseModelPackageName, String modelOutputDir) {
        super(modelPackageName, baseModelPackageName, modelOutputDir);

        this.importTemplate =
                "import com.jservice.framework.idWorker.Sequence;\n" +
                "import com.jservice.framework.spring.SpringContextUtil;\n" +
                "import com.jservice.model.%s;\n" +
                "import org.springframework.stereotype.Service;\n" +
                "import org.springframework.transaction.annotation.Transactional;\n" +
                "import javax.annotation.Resource;\n";

        this.classDefineTemplate = "/**\n" +
                " * %sQuery\n" +
                " *\n" +
                " * @author AutoGenerated\n" +
                " */\n" +
                "@Service(\"%sQuery\")\n" +
                "@Transactional(readOnly = true)\n" +
                "public class %sQuery extends JBaseQuery{%n%n";

        this.daoTemplate = "public static final %s DAO = new %s();\n" +
                "\n" +
                "    public static %sQuery me() {\n" +
                "        return SpringContextUtil.getBean(\"%sQuery\");\n" +
                "    }\n" +
                "\n" +
                "    @Resource\n" +
                "    private Sequence idWorker;";

    }

    @Override
    protected void genImport(TableMeta tableMeta, StringBuilder ret) {
        ret.append(String.format(importTemplate, tableMeta.modelName));
    }

    @Override
    protected void genClassDefine(TableMeta tableMeta, StringBuilder ret) {
        ret.append(String.format(classDefineTemplate, tableMeta.modelName, StrKit.firstCharToLowerCase(tableMeta.modelName),tableMeta.modelName));
    }

    @Override
    protected void genDao(TableMeta tableMeta, StringBuilder ret) {
        ret.append(String.format(daoTemplate, tableMeta.modelName,tableMeta.modelName,tableMeta.modelName,StrKit.firstCharToLowerCase(tableMeta.modelName)));
    }

    public void generate(List<TableMeta> tableMetas) {
        System.out.println("Generate Query ...");
        for (TableMeta tableMeta : tableMetas)
            genModelContent(tableMeta);
        wirtToFile(tableMetas);
    }


    protected void wirtToFile(TableMeta tableMeta) throws IOException {
        File dir = new File(modelOutputDir);
        if (!dir.exists())
            dir.mkdirs();

        String target = modelOutputDir + File.separator +tableMeta.modelName + "Query.java";

        File file = new File(target);
        if (file.exists()) {
            return ;	// 若 Query 存在，不覆盖
        }
        FileWriter fw = new FileWriter(file);
        try {
            fw.write(tableMeta.modelContent);
        }
        finally {
            fw.close();
        }
    }
}
